home *** CD-ROM | disk | FTP | other *** search
/ HyperLib 1997 Winter - Disc 1 / HYPERLIB-1997-Winter-CD1.ISO.7z / HYPERLIB-1997-Winter-CD1.ISO / オンラインウェア / PRG / CLImax 1.0.sit / CLImax 1.0 / AppleScript and CLImax / AppleScript troubleshooting < prev    next >
Text File  |  1996-11-07  |  8KB  |  159 lines

  1. _______________________________________________________
  2.  
  3. Common problems with AppleScript
  4.            (or, Why Doesn't This WORK!?)
  5.  
  6. ... from the simple to the very difficult.
  7.  
  8. _______________________________________________________
  9.  
  10. This is a collection of some of the common problems I've encountered (and seen others encounter) while using CLImax. Obviously not every possible error is listed here, but there are enough to give you a feel for what you can and can't do with AppleScript.
  11.  
  12. n.b: While I would like to hear of any bugs you might find in CLImax, PLEASE don't e-mail me with AppleScript questions!!!  :-)  There are several excellent mailing lists, newsgroups, and FTP sites dedicated to AppleScript; see the file メAppleScript on the Netモ for more info.  Thanks.
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21. Frequency:  **
  22. Difficulty: *
  23. Problem:    open folder games of startup disk
  24.             tell application finder to activate
  25.  
  26. Error:      The variable games is not defined.
  27.             The variable finder is not defined.
  28.  
  29. Should be:  open folder "games" of startup disk
  30.             tell application "finder" to activate
  31.  
  32. Explanation:  Use quotation marks when referring to items by name.  :)
  33.  
  34. ---
  35.  
  36. Frequency:  ****
  37. Difficulty: ***
  38. Problem:    items of front window whose kind is alias
  39. Error:      Always returns {}!
  40. Should be:  items of front window whose kind is "alias"
  41.  
  42. Explanation:
  43.  
  44. The quotation marks aren't as obviously necessary here, but if you select an alias and execute メkind of selectionモ, you'll see that the kind property actually returns a character string, rather than an enumerated type.  So that's what you need to pass to the whose clause in this case.
  45.  
  46. ---
  47.  
  48. Frequency:  ******
  49. Difficulty: **
  50. Problem:    file creator of item "Read Me" of front window
  51. Error:      Can't get file creator of item "Read Me" of window 1.
  52. Should be:  creator type of item "Read Me" of front window
  53.  
  54. Explanation:
  55.  
  56. Before you look for other problems, always make sure that you're asking for something that the app's dictionary accepts.  メFile creatorモ is meaningless to the Finder.
  57.  
  58. This is probably the most common type of error; even if you wrote the application's dictionary yourself, you can still make this kind of mistake.  (cough cough)   ;>
  59.  
  60.  
  61. ---
  62.  
  63. Frequency:  **
  64. Difficulty: *
  65. Problem:    the fourth folder of the front window
  66. Error:      Can't get . Invalid index.
  67.  
  68. Explanation:
  69.  
  70. The front window probably doesn't have four folders in it.  It may have fewer, or it may not be a window that's capable of holding folders, such as the "about this macintosh" window.
  71.  
  72. ---
  73.  
  74. Frequency:  **
  75. Difficulty: ***
  76. Problem:    tell application "Script Editor" to open file "Finder" of system folder
  77. Error 1:    "No user interaction allowed."
  78.  
  79. Explanation:
  80.  
  81. AppleScript needs to know where Script Editor is in order to launch it.  To this end, it posts a notification, which CLImax will catch, and then bring itself to the foreground to display the file search dialog.
  82.  
  83. If CLImax is running as a daemon, however, AppleScript can't post the notification, and you get the error メNo user interaction allowedモ, or sometimes just メAn error of type -610 has occurred.モ
  84.  
  85. This problem doesn't occur if the application is already running.
  86.  
  87. ---
  88.  
  89. Frequency:  ****
  90. Difficulty: ***
  91. Problem:    tell application "Script Editor" to open file "Finder" of system folder
  92. Error 2:    Can't get file "Finder" of system folder.
  93. Solution:   file "Finder" of system folder as alias
  94.             -- (cmd-tab to Script Editor)
  95.             open result
  96.  
  97. Alternate solution:  dictionary for "Finder"  -- while talking to CLImax
  98.  
  99. Explanation:
  100.  
  101. This could be described as a context problem.  When you start talking to Script Editor with the メtellモ command, you lose a lot of the capabilities provided by the Finder's dictionary.  Basically, what's happening is that you're asking Script Editor to resolve the Finder objects メfileモ and メsystem folderモ, which it's incapable of doing, because it doesn't know what they are.
  102.  
  103. The solution is to break it up into two commands, as listed above. The first one has the Finder resolve the reference, and coerces it into an alias.  (Aliases are very portable, coerceable items and generally will work for anything that needs any kind of file reference.)  The second command (taking advantage of CLImax's ability to set the default target application) tells Script Editor to open the result of the previous expression -- which is the alias to the Finder.
  104.  
  105. Remember, CLImax is designed with single commands in mind, which may seem unfamiliar to most experienced AppleScript users.  As a general rule, try to use several short commands and the メresultモ variable, rather than one long command.  And don't forget that CLImax can quickly and easily switch the default target application!  There's really almost no need to ever use メtellモ outside of an event handler.
  106.  
  107. (In case you didn't know, telling Script Editor to open an application will cause it to open that app's scripting dictionary, which is a very useful capability.  This is exactly what CLImax does with its メdictionaryモ command, and in fact since this command is so easy to use thereユs hardly any reason for doing the above at all anyway. :)
  108.  
  109.  
  110. ---
  111.  
  112. Frequency:  ***
  113. Difficulty: *****
  114. Problem:    items in system folder whose creator type is "MACS"
  115. Error:      Can't get every item of system folder whose creator type = "MACS".
  116. Should be:  files in system folder whose creator type is "MACS"
  117.  
  118. Explanation:
  119.  
  120. The class メitemsモ includes both folders and files, among other things.  During the whose clause resolution, the Finder looks at every item, tries to obtain its creator type property, and then compares that property to the desired one.  But, as it goes through the list of items, for this case at least, it will encounter both files and folders.  Files have a メcreator typeモ property; folders don't.  The very first time it encounters a folder in the item list, AppleScript will fail while trying to obtain the creator type property.  The unhappy result is the very nondescriptive error listed above.  The solution: tell it to look only at files, not all items; in other words, only objects that have the particular property you're looking for.
  121.  
  122. Problems of this sort can be extremely puzzling if the application doesn't encounter a メbad itemモ until late in the list, because you may be able to find the first few items that satisfy the criteria (if you reference them with item 1, item 2, etc) but not a list of all the items at once.
  123.  
  124.  
  125. ---
  126.  
  127. Frequency:  ***
  128. Difficulty: ***
  129. Problem:    open thesefiles and thosefiles
  130. Error:      Can't make { ... list ... } into a Boolean.
  131. Solution:   open thesefiles & thosefiles
  132.  
  133. Explanation:
  134.  
  135. メandモ is a Boolean operator, which works only on logical values -- ie, true and false.  In this case the intention was to join the lists, so the list concatenation operator (&) should be used instead.
  136.  
  137. (Yes, AppleScript is still a computer language and *not* English, no matter how much it tries to pretend otherwise. :-)
  138.  
  139.  
  140. ---
  141.  
  142. Frequency:  *        (but just enough to be frustrating :)
  143. Difficulty: ???
  144. Problem:    windows whose items = {}
  145. Error:      Wrong keyword for a special function.
  146. Solution:   ???
  147.  
  148. This is one that I'll admit has eluded me.  Most of the メwhoseモ constructs I've tried work very well.  However, using メcontentsモ (or メitemsモ) as a property for the whose clause doesn't appear to work with the Finder. CLImax's メinfo forモ command returns the following:
  149.  
  150.      info for (windows whose contents = {})
  151.      {type:reference, class id:"obj ", value:every window whose contents = {}, raw data length:188, raw data:{key form:test, desired class:window, container:current application, key data:contents = {}}}
  152.  
  153. Okay. Now, as far as I can tell, this looks right!  So what's the problem?
  154.  
  155. I can't say for sure, but it may be that since メcontentsモ, as it's used here, is technically a property of objects in general, and not explicitly defined (for this context) in the Finder's dictionary, it's possible that the Finder's object support functions weren't written with the capability to explicitly test the メitemsモ property.  When they get called to do so, that might be what causes the error. I'm not sure, though.  And it's made a little more difficult by the fact that メitemモ means about five different things in different contexts.  (Sounds like a good question for the AppleScript mailing list! :)
  156.  
  157. ---
  158.  
  159.